package com.lagou.ModelTest4.Second;

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

public class ClientStringTest {
    public static void main(String[] args) {
        PrintStream ps=null;
        Socket s=null;
        try {
            //1.构造Socket还有主机号和端口号
            s = new Socket("127.0.0.1", 6666);
            System.out.println("服务器"+s.getInetAddress()+"连接成功!");
            //2.输入输出流 发送给服务器信息
            //3.不断地从键盘输入
            while (true) {
                System.out.println("请输入要发送的内容:");
                Scanner sc = new Scanner(System.in);
                String str2 = sc.next();
                ps = new PrintStream(s.getOutputStream());
                ps.println(str2);
                //直到输入“end”时循环结束
                if("end".equalsIgnoreCase(str2)){
                    System.out.println("聊天结束!");
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        //4.关闭并释放有关资源
        if(null!=ps){
                ps.close();
        }
        if(null!=s){
            try{
                s.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
}
